package com;

import com.amazonaws.services.s3.internal.Mimetypes;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectTagging;
import com.amazonaws.services.s3.transfer.*;
import com.amazonaws.services.s3.transfer.internal.MyUploadImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.util.Collection;
import java.util.List;

/**
 * 上传队列
 */
public class UploadQueue {

    public static UploadQueue create() {
        return new UploadQueue();
    }

    /**
     * 原队列
     */
    private List<File> list;
    private MyTransferManager transferManager;
    private ConsoleProgressBar consolePro;

    public UploadQueue() {

    }

    public UploadQueue setTransferManager(MyTransferManager transferManager) {
        this.transferManager = transferManager;
        return this;
    }

    public void startUpload(List<File> list) {
        this.list = list;
        consolePro = new ConsoleProgressBar(0, 100, 30);
        consolePro.startChar = "%n% ";
        upload();
    }

    private void upload() {

        System.out.println("上传数量 len = " + list.size());

        MultipleFileUpload xfer = transferManager.uploadFileList(Config.dataManage.getPackCmd().bucketName,
                Config.dataManage.getPackCmd().rootPath,
                new File(Config.dataManage.getPackCmd().parentPath), list, (file, objectMetadata) -> {
                    objectMetadata.setContentLength(file.length());
                    objectMetadata.setContentType(Mimetypes.getInstance().getMimetype(file));
                }, new ObjectTaggingProvider() {
                    @Override
                    public ObjectTagging provideObjectTags(UploadContext uploadContext) {

                        return null;
                    }
                }, CannedAccessControlList.PublicRead);

        showMultiUploadProgress(xfer);
        // 等待完成
        XferMgrProgress.waitForCompletion(xfer);

    }

    // Prints progress of a multiple file upload while waiting for it to finish.
    private void showMultiUploadProgress(MultipleFileUpload multi_upload) {
        // print the upload's human-readable description
        System.out.println(multi_upload.getDescription());

        Collection<? extends Upload> sub_xfers = multi_upload.getSubTransfers();

        String fileName;
        do {
            fileName = "";
            for (Upload u : sub_xfers) {
//                if (u.isDone()) {
//                    Transfer.TransferState xfer_state = u.getState();
//                    System.out.println("  " + xfer_state);
//                } else {
//                    TransferProgress progress = u.getProgress();
//                    double uploadPct = progress.getPercentTransferred();
//                    if (StringUtils.isEmpty(fileName)) {
//                        fileName = ((MyUploadImpl)u).getPutObjectRequest().getKey()
//                                +" "+((int)uploadPct)+"%";
//                    } else {
//                        fileName += ", "+((MyUploadImpl)u).getPutObjectRequest().getKey()
//                                +" "+((int)uploadPct)+"%";
//                    }
//                }
            }
            TransferProgress progress = multi_upload.getProgress();
            double pct = progress.getPercentTransferred();

//            consolePro.details = "  ["+fileName+"]";
            consolePro.details = "";
            consolePro.show((long) pct);
            // wait a bit before the next update.
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                LogFactory.getLog(getClass()).error(null, e);
                return;
            }
        } while (!multi_upload.isDone());
        // print the final state of the transfer.
        Transfer.TransferState xfer_state = multi_upload.getState();
        consolePro.details = "";
        consolePro.show(100);
        System.out.println("\nMultipleFileUpload " + xfer_state);
    }


}
